﻿using Api.Shared.Model.Abb;
using Api.Shared.Model.Yanji;
using Api.Shared.MyPlc.S7;
using Api.Shared.Sugar;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using KxApp.Utils;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KxApp.ViewModels
{
    [ExposedService(Lifetime.Singleton)]
    public partial class ScanViewModel : ObservableObject
    {
        public ScanViewModel(IS7ConnService s7, [FromKeyedServices(nameof(SqlName.mysqlST))] ISugarClientService mysqlService)
        {
            this.s7 = s7;
            this.mysqlService = mysqlService;
        }

        [ObservableProperty]
        private string twoCode = string.Empty;

        [ObservableProperty]
        private string oneCode = string.Empty;

        [ObservableProperty]
        private ObservableCollection<AbbCode> abbCodes = new();
        private readonly IS7ConnService s7;
        private readonly ISugarClientService mysqlService;

        [RelayCommand]
        public async Task Submit()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(TwoCode) || string.IsNullOrWhiteSpace(TwoCode))
                {
                    net8Wpf.Core.Diag.PopupBox.Show("请填写完整的双顶出和单顶出代码", null, 2, "红");
                    return;
                }
                var bool1 = ParseStringClz.ParseString(TwoCode);
                if (!bool1)
                {
                    net8Wpf.Core.Diag.PopupBox.Show("填写信息没有空格", null, 2, "红");
                    return;
                }
                AbbCode abbCode = new()
                {
                    TwoCode = TwoCode,
                    OneCode = OneCode,
                    Description = $"{TwoCode} ,{OneCode}",
                    Type1 = ParseStringClz.StrType1,
                    Type2 = ParseStringClz.StrType2,
                    Type3 = ParseStringClz.StrType3,
                    Type4 = ParseStringClz.StrType4,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                    CreateUser = "admin",
                    UpdateUser = "admin"
                };
                AbbApiData apiData = new()
                {
                    Short1 = (short)abbCode.CodeType1, //
                    Short2 = (short)abbCode.CodeType2,
                    Short3 = (short)abbCode.Type3,
                    Short4 = (short)abbCode.Type4
                };

                try
                {
                    AbbStr abbStr = new() { Str1 = OneCode, Str2 = TwoCode };
                    await s7.S7Plc.WriteClassAsync(apiData, 31, 0);
                    await s7.S7Plc.WriteClassAsync(abbStr, 507, 0);
                    var a = await mysqlService.Db.Insertable(abbCode).ExecuteCommandAsync();

                }
                catch (Exception ex)
                {
                    net8Wpf.Core.Diag.PopupBox.Show($"写入Plc数据失败:{ex.Message}", null, 2, "红");
                    return;
                }
                AbbCodes.Add(abbCode);

            }
            catch (Exception ex)
            {
                net8Wpf.Core.Diag.PopupBox.Show($"{ex.Message}", null, 2, "红");
            }
        }
    }
}
